using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    // 游戏的开始结束判定

    // 存储所有的小鸟
    public Bird[] birdList;
    // 设置index
    private int index = -1;

    // 判断结束条件一 ： 记录小猪数量
    private int pigTotalCount;
    private int pigDeadCount;
    // 得到Camera跟随目标
    private FllowTarget cameraFollowTarget;
    
    // 设置单例模式
    public static GameManager Instance {get; private set;}
    // 单例模式赋值
    private void Awake()
    {
        Instance = this;
        pigDeadCount = 0;
    }

    public void LoadNextBird()
    {
        index++;
        if (index >= birdList.Length)
        {
            GameEnd();
        }
        else
        {
            birdList[index].GoStage(Slingshot.Instance.getCenterPosition());
            cameraFollowTarget.SetTraget(birdList[index].transform);
        }
        
    }

    // 判断小猪死亡
    public void OnPigDead()
    {
        pigDeadCount++;

        if (pigDeadCount >= pigTotalCount)
        {
            GameEnd();
        }
    }

    // 判断游戏结束
    private void GameEnd()
    {
        print("Game End~");
    }


    // Start is called before the first frame update
    void Start()
    {
        birdList = FindObjectsByType<Bird>(FindObjectsSortMode.None);
        // 小猪数量
        pigTotalCount = (FindObjectsByType<Pig>(FindObjectsSortMode.None)).Length;
        cameraFollowTarget = Camera.main.GetComponent<FllowTarget>();

        LoadNextBird();
    }


    // Update is called once per frame
    void Update()
    {
        
    }
}
